package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.entity.Comment;
import zsc.nine.onlinemall.service.CommentService;

import java.util.List;

/**
 * @ClassName: CommentController
 * @Description: Controller of comment operation
 * @Author: Zainus
 * @CreateDate: Jul.28, 2021
 * @UpdateDate: Jul.28, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: CommentController
 * @描述: 评论管理控制类接口
 * @创建人: 叶增钥
 * @创建时间: 2021年7月28日
 * @更新时间: 2021年7月28日
 * @版本: [v1.0]
 */
@RequestMapping("/Comment")
@RestController
public class CommentController {


    @Autowired
    CommentService commentService;

    //删：根据id
    @PostMapping("/Delete")
    public String DeleteComment(@RequestParam(value = "Id",required = false) String id){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("DeleteCommentResult", "Fail");

        /*后端校验数据合法性*/
        if (id == null) {
            jsonObject.put("DeleteCommentResult", "Parameter-Null");

        } else if (id.trim().isEmpty()) {
            jsonObject.put("DeleteCommentResult", "Empty");
        } else {
            if(commentService.deleteComment(Integer.parseInt(id))) {
                jsonObject.put("DeleteCommentResult", "Success");
            }
        }
        return jsonObject.toJSONString();
    }

    //管理员后台查看所有(评论)
    @PostMapping("/FindAllComment")
    public String FindAllComment(){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("FindAllCommentResult", "Fail");

        List<Comment> commentList = commentService.findAllComment();
        jsonObject.put("FindAllCommentResult", "Success");
        jsonObject.put("FindAllCommentObjectResult", commentList);

        return jsonObject.toJSONString();
    }

}
